package com.enniswu.weitter.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;

import com.enniswu.weitter.R;
import com.enniswu.weitter.data.local.SPSupport;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class ImageUtil {

    public static String PORTRAIT_MALE = "MalePortrait";
    public static String PORTRAIT_FEMALE = "FemalePortrait";

    public static String getPath(Context context, String filename) {
        return context.getFilesDir() + File.separator + filename;
    }

    public static Bitmap drawableToBitmap(Context context, @DrawableRes int resId) {
        return BitmapFactory.decodeResource(context.getResources(), resId);
    }

    public static byte[] bitmapToBytes(Context context, Bitmap bitmap) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] bytes = baos.toByteArray();
        try {
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bytes;
    }

    public static File bytesToFile(Context context, byte[] bytes, @Nullable String filename) {
        if (filename == null)
            filename = String.valueOf(System.currentTimeMillis());
        filename += ".png";
        File file = new File(getPath(context, filename));
        if (!file.exists()) {
            FileOutputStream fos = null;
            try {
                fos = context.openFileOutput(filename, Context.MODE_PRIVATE);
                fos.write(bytes);
                fos.flush();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (fos != null)
                        fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return file;
    }

    public static void saveDefaultPortrait(Context context) {
        if (!SPSupport.getInstance().getFirst())
            return;
        bytesToFile(context, bitmapToBytes(context, drawableToBitmap(context,
                R.drawable.ic_portrait_male)), PORTRAIT_MALE);
        bytesToFile(context, bitmapToBytes(context, drawableToBitmap(context,
                R.drawable.ic_portrait_female)), PORTRAIT_FEMALE);
    }

    public static Uri getCropUri(Context context) {
        File file = new File(context.getExternalCacheDir(),
                System.currentTimeMillis() + ".jpg");
        try {
            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Uri.fromFile(file);
    }
}
